import React, {Component} from 'react';
import ReactDOM from 'react-dom';
import {createPage, ajax} from 'nc-lightapp-front';
import {buttonClick, initTemplate, afterEvent, tableModelConfirm, onRowDoubleClick} from './events/index';
import requestApi from './requestApi'
import './index.less';
import {
    ProfileStyle,
    ProfileHead,
    ProfileBody,
    BodyLeft,
    BodyRight,
    HeadCenterCustom,
    EmptyArea,
    ButtonGroup
} from 'ssccommon/components/profile';
import {EditTable} from 'ssccommon/components/table';
import { setTable } from 'ssccommon/utils/setComponentUsed';

class LinkBillInfo extends Component {
    constructor(props) {
        super(props);
        setTable(props, 'head');
        let url = window.location.href;
        this.state = {
            sourceData: '',
            pk_ntbparadimvo : url.split('=')[1]
        };
    }
    componentDidMount() {
        requestApi.query({
            data : {
                "ntbpk" : this.state.pk_ntbparadimvo
            },
            success: (data) => {
                if (data && data['head'] && data['head'].rows) {
                    let row = data['head'].rows[0];
                    //let meta = this.props.meta.getMeta();

                    //let arr = Object.keys(meta);
                    //if (arr.length == 0) {
                    let meta = {
                        head: {
                            moduletype: 'table',
                            items: [
                                {
                                    visible: true,
                                    label: '维度0',//'业务单元',
                                    attrcode: 'qryobj0',
                                    itemtype: 'input',
                                    col: 12,
                                    initialvalue: {
                                        value: '',
                                        display: null
                                    }},
                                {
                                    visible: true,
                                    label: '维度1',//'收支项目'
                                    attrcode: 'qryobj1',
                                    itemtype: 'input',
                                    col: 12,
                                    initialvalue: {
                                        value: '',
                                        display: null
                                    }},
                                {
                                    visible: false,
                                    label: "维度2",
                                    attrcode: 'qryobj2',
                                    itemtype: 'input',
                                    col: 12,
                                    initialvalue: {
                                        value: '',
                                        display: null
                                    }},
                                {
                                    visible: false,
                                    label: "维度3",//'收支项目'
                                    attrcode: 'qryobj3',
                                    itemtype: 'input',
                                    col: 12,
                                    initialvalue: {
                                        value: '',
                                        display: null
                                    }},
                                {
                                    visible: false,
                                    label: "维度4",//'收支项目'
                                    attrcode: 'qryobj4',
                                    itemtype: 'input',
                                    col: 12,
                                    initialvalue: {
                                        value: '',
                                        display: null
                                    }},
                                {
                                    visible: true,
                                    label: '单据类型',//'单据类型',
                                    attrcode: 'pk_billtype',
                                    itemtype: 'input',
                                    col: 12,
                                    initialvalue: {
                                        value: '',
                                        display: null
                                    }},
                                {
                                    visible: true,
                                    label: '单据编号',//'单据编号',
                                    attrcode: 'djbh',
                                    itemtype: 'input',
                                    col: 12,
                                    initialvalue: {
                                        value: '',
                                        display: null
                                    }},
                                {
                                    visible: true,
                                    label: '单据日期',//'单据日期',
                                    attrcode: 'djrq',
                                    itemtype: 'datepicker',
                                    col: 12,
                                    initialvalue: {
                                        value: '',
                                        display: null
                                    }},
                                {
                                    visible: true,
                                    label: '本币发生',//'本币发生',
                                    attrcode: 'loc',
                                    itemtype: 'input',
                                    col: 12,
                                    initialvalue: {
                                        value: '',
                                        display: null
                                    }},
                                {
                                    visible: true,
                                    label: '原币发生',//'原币发生',
                                    attrcode: 'ori',
                                    itemtype: 'input',
                                    col: 12,
                                    initialvalue: {
                                        value: '',
                                        display: null
                                    }},
                                {
                                    visible: true,
                                    label: '集团发生',//'集团发生',
                                    attrcode: 'gr_loc',
                                    itemtype: 'input',
                                    col: 12,
                                    initialvalue: {
                                        value: '',
                                        display: null
                                    }},
                                {
                                    visible: true,
                                    label: '全局发生',//'全局发生',
                                    attrcode: 'gl_loc',
                                    itemtype: 'input',
                                    col: 12,
                                    initialvalue: {
                                        value: '',
                                        display: null
                                    }
                                }]
                        }
                    };
                    
                    meta["head"].items.map(one =>{
                        if (one.attrcode.startsWith("qryobj0")) {
                            if(row.values.qryobj0.value) {
                                one.label = row.values.qryobj0type.value;
                                one.visible = true;
                            } else {
                                one.visible = false;
                            }
                        } else if (one.attrcode.startsWith("qryobj1")) {
                            if(row.values.qryobj1.value) {
                                one.label = row.values.qryobj1type.value;
                                one.visible = true;
                            } else {
                                one.visible = false;
                            }
                        } else if (one.attrcode.startsWith("qryobj2")) {
                            if(row.values.qryobj2.value) {
                                one.label = row.values.qryobj2type.value;
                                one.visible = true;
                            } else {
                                one.visible = false;
                            }
                        } else if (one.attrcode.startsWith("qryobj3")) {
                            if(row.values.qryobj3.value) {
                                one.label = row.values.qryobj3type.value;
                                one.visible = true;
                            } else {
                                one.visible = false;
                            }
                        } else if (one.attrcode.startsWith("qryobj4")) {
                            if(row.values.qryobj4.value) {
                                one.label = row.values.qryobj4type.value;
                                one.visible = true;
                            } else {
                                one.visible = false;
                            }
                        }
                    });

                    this.props.meta.setMeta(meta);
                }
                data && this.props.table.setAllTableData('head', data['head']);
            }
        })
    }


    render() {
        const {editTable, table, button} = this.props;
        const {createEditTable} = editTable;
        const {createButton} = button;
        const {createSimpleTable} = table;
        let multiLang = this.props.MutiInit.getIntl(2011); //this.moduleId
        return (
            <ProfileStyle
                layout="singleTable"
                {...this.props}
            >
                {/*页面头*/}
                {/*"201103PZYM-0004": "预算联查单据",*/}
                <ProfileHead
                    title={multiLang && multiLang.get('201103PZYM-0004')}
                >
                </ProfileHead>

                {/*页面体*/}
                <ProfileBody>
                    <EmptyArea>
                        {createSimpleTable('head', {
                            onAfterEvent: afterEvent,
                            onRowDoubleClick : onRowDoubleClick,
                            showIndex:true
                        })}
                    </EmptyArea>
                </ProfileBody>
            </ProfileStyle>
        )
    }
}

LinkBillInfo = createPage({
    initTemplate: initTemplate,
    mutiLangCode: '2011'
})(LinkBillInfo);

ReactDOM.render(<LinkBillInfo/>, document.querySelector('#app'));